<script setup>
import { ref, defineEmits, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { updateApi, setArrivalApi } from '@/api/warehousingentry/warehousingEntryDetails'
import { getSkuInfoApi } from '@/api/item'
import eventBus from '@/utils/eventBus'
const emit = defineEmits(['refresh'])
const ruleFormRef = ref(null)
const rules = ref({
  realArrivalTime: [
    { required: true, message: '请选择到达时间', trigger: 'blur' }
  ]
})
const selectedRows = ref([])
const dialogVisible = ref(false)
const realArrivalTime = ref('')

const resetForm = () => {
  dialogVisible.value = false
  selectedRows.value = []
  realArrivalTime.value = ''
}
// 确认更新到达时间
const confirm = async () => {
  if (!realArrivalTime.value) {
    ElMessage.warning('请选择到达时间')
    return
  }
  // 构造更新数据
  const updateData = selectedRows.value.map(row => ({
    id: row.id,
    realArrivalTime: realArrivalTime.value
  }))
  try {
    const res = await setArrivalApi(updateData)
    if (res.code) {
      ElMessage.success('操作完成!')
      resetForm()
      emit('refresh')
      eventBus.emit('refreshunloadPage')
    } else {
      ElMessage.error('操作失败！')
    }
  } catch (error) {
    ElMessage.error('操作异常！')
  }
}
const itemInfo = ref()
const getItemList = async () => {
  let res = await getSkuInfoApi()
  itemInfo.value = res
}
onMounted(() => {
  getItemList()
})
defineExpose({
  itemInfo,
  dialogVisible,
  selectedRows
})
</script>

<template>
  <el-dialog v-model="dialogVisible" title="入库明细信息" width="600px">
    <el-form :model="realArrivalTime" label-width="130px" ref="ruleFormRef" :rules="rules">
      <el-form-item label="到达时间">
        <el-date-picker v-model="realArrivalTime" type="datetime" placeholder="请选择到达时间" style="width: 100%"
          value-format="YYYY-MM-DD HH:mm:ss" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>